package com.example.gymmanagement.controller;

import com.example.gymmanagement.Utils.Result;
import com.example.gymmanagement.Utils.StatusCode;
import com.example.gymmanagement.domain.Img;
import com.example.gymmanagement.service.ImgService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *图片管理
 */
@RestController
@RequestMapping("/img")
public class ImgController {
    @Autowired
    private ImgService imgService;

    @ApiOperation("按id找图片")
    @GetMapping("/selectById")
    public Result selectById(int id,@RequestHeader("Authorization") String token) {
        Img img = imgService.selectById(id);
        String msg = (img!=null ?"查询成功":"未找到该图片");
        return new Result(img!=null? StatusCode.GET_OK:StatusCode.GET_ERR,img,msg);
    }

    @ApiOperation("保存图片")
    @PostMapping("/save")
    public Result save(@RequestBody Img img,@RequestHeader("Authorization") String token) {
        boolean flag = imgService.insert(img);
        String msg = (flag?"存储图片成功":"存储图片失败,可能该id已存在");
        return new Result(flag?StatusCode.SAVE_OK:StatusCode.SAVE_ERR,img,msg);
    }

    @ApiOperation("按id删除图片")
    @GetMapping("/deleteById")
    public Result deleteById(int id,@RequestHeader("Authorization") String token) {
        boolean flag = imgService.deleteById(id);
        String msg = (flag?"删除成功":"未找到该图片id");
        return new Result(flag? StatusCode.DELETE_OK:StatusCode.DELETE_ERR,flag,msg);
    }

    @ApiOperation("修改图片")
    @PostMapping("/updateImg")
    public Result updateImg(@RequestBody Img img,@RequestHeader("Authorization") String token){
        boolean flag = imgService.updateImg(img);
        String msg = (flag?"修改图片成功":"修改图片失败,id不存在");
        return new Result(flag?StatusCode.UPDATE_OK:StatusCode.UPDATE_ERR,img,msg);
    }
}
